import { Component, UITransform, Vec3 } from "cc";
import { BattleRoleUI } from "../BattleRoleUI";
import { MathUtil } from "../../../new/util/MathUtil";

/**
 * 移动组件
 */
export class MoveComp extends Component {

    targetPoint: Vec3;
    transform: UITransform;
    isStop: boolean = true;
    /**停下的距离 */
    dic: number = 0;
    /**走到目标点回调 */
    callBack: Function
    protected update(dt: number): void {
        if (this.isStop) return;
        if (MathUtil.getDistance(this.targetPoint, this.node.position) <= this.dic) {
            if (this.callBack) {
                this.callBack.call(this);
                this.callBack = null;
            }
            this.isStop = true;
            return;
        }
        let angle = MathUtil.getAngle(this.node.position, this.targetPoint);
        let x = Math.cos(angle) * 5
        let y = Math.sin(angle) * 5
        this.node.translate(new Vec3(x, y, 0));
    }

    stopMove(): void {
        this.isStop = true;
    }

    /**继续原来的移动 */
    doMove(): void {
        this.isStop = false;
    }

    moveToPoint(point: Vec3, dic: number = 0, callBack: Function): void {
        this.isStop = false;
        this.targetPoint = point;
        this.dic = dic;
        this.callBack = callBack;
    }
}